﻿#include "mainwindow.h"
#include <QApplication>
#include <QStandardPaths>
#include <QSplashScreen>
#include <QSettings>


std::shared_ptr<spdlog::logger> logger  = spdlog::daily_logger_mt("daily_logger", "logs/daily.txt", 0, 00);



// 编译器链接Windows调试帮助库
#pragma comment(lib, "Dbghelp.lib")
#include <WinSock2.h>
#include <DbgHelp.h>
#include <windows.h>
#include <winnt.h>
// 创建dump文件的函数
void CreateMiniDump(EXCEPTION_POINTERS* pep, char* strDumpFile)
{
    HANDLE hFile = CreateFileA(strDumpFile, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
    if (hFile!= NULL && hFile!= INVALID_HANDLE_VALUE)
    {
        MINIDUMP_EXCEPTION_INFORMATION mdei;
        mdei.ThreadId = GetCurrentThreadId();
        mdei.ExceptionPointers = pep;
        mdei.ClientPointers = TRUE;
        MINIDUMP_TYPE mdt = MiniDumpWithDataSegs;
        BOOL rv = MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hFile, mdt, &mdei, 0, 0);
        if (rv)
        {
            // 可添加日志记录创建成功
        }
        else
        {
            // 可添加日志记录创建失败及错误码
        }
        CloseHandle(hFile);
    }
}

// 异常处理函数
LONG WINAPI MyCustomFilter(EXCEPTION_POINTERS* pep)
{
    QString strDumpFile;
    strDumpFile.sprintf("Dump_%s_%s.dmp", QDate::currentDate().toString("yyyyMMdd").toStdString().c_str(), QTime::currentTime().toString("HHmmss").toStdString().c_str());
    CreateMiniDump(pep, strDumpFile.toUtf8().data());
    return EXCEPTION_EXECUTE_HANDLER;
}



int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
//    // 设置异常处理函数
//    SetUnhandledExceptionFilter(MyCustomFilter);


//    QFile styleFile(QCoreApplication::applicationDirPath() + "/para/dark.qss");
//    if(styleFile.open(QFile::ReadOnly))
//    {
//        qApp->setStyleSheet(styleFile.readAll());
//        styleFile.close();
//    }


    QString m_para_dir = QCoreApplication::applicationDirPath() + "/para";
    QString path_ini;
    path_ini = m_para_dir + "/para.ini";
    //创建配置文件操作对象
    QSettings* m_settings_para = new QSettings(path_ini, QSettings::IniFormat);
    m_settings_para->setIniCodec("UTF-8");
    int font_size = m_settings_para->value("para/font_size", 12).toInt();
    delete m_settings_para;



    QString path   = QCoreApplication::applicationDirPath() + "/picture/background.svg";

    QPixmap pixmap(path);
    QSplashScreen *splash = new QSplashScreen;

    QFont font("Microsoft YaHei",50);


    splash->setFont(font);
    splash->showMessage("start...", Qt::AlignCenter, Qt::white);	//显示文字、对齐方式、文字颜色
    splash->setPixmap(pixmap);										//绑定图片
    splash->show();                            						//显示启动画面
    logger->flush_on(spdlog::level::info);


    MainWindow w;

    w.show();
    splash->finish(&w);											//关闭启动界面
    delete splash;

    return a.exec();
}
